//
//  XJCommunityHistoryDetailViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/1.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  详情

#import "XJCommunityHistoryDetailViewController.h"
#import "XJURLConst.h"
@interface XJCommunityHistoryDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSMutableArray *dataArray;

@property(nonatomic, strong) UIView *footerView;

@property(nonatomic, strong) NSMutableArray *imgsArray; //装iv。方便后面那到他们进行赋值 这种思想可以有。 你只要能拿到他你不就可以赋值了吗。 ✅

/**记录下服务id 取消接口的请求参数*/
@property (nonatomic, copy) NSString *service_id;

@end

@implementation XJCommunityHistoryDetailViewController

#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight - XJTopNavH) style:(UITableViewStylePlain)];
        
        // 取消下划线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

// h: 屏高 -  44*6(6个cell)
- (UIView *)footerView {
    if (!_footerView) {
        
        CGFloat kFooterVeiw_h = KHeight - 44*6;
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, kFooterVeiw_h)];
        
        
        CGFloat kLeftMargin = ADAPTWidth(18);
        
        // 线
        UIView *line = [UIView new];
        line.frame = CGRectMake(kLeftMargin, 1, KWidth - kLeftMargin *2, 0.5);
        line.backgroundColor = [UIColor lightGrayColor];
        [_footerView addSubview:line];
        
        // 描述内容
        UILabel *desLabel = [UILabel new];
        desLabel.frame = CGRectMake(kLeftMargin, 10, KWidth - kLeftMargin*2, 20);
        desLabel.text = @"描述内容";
        [_footerView addSubview:desLabel];
        
        
        // icon
        UIView *imgsBgView = [[UIView alloc] initWithFrame:CGRectMake(kLeftMargin, desLabel.rb_bottom+5, KWidth - kLeftMargin*2, 150)];
//        imgsBgView.backgroundColor = [UIColor redColor];
        [_footerView addSubview:imgsBgView];

        // 中间间距 5
        CGFloat k5 = 5;
        CGFloat iv_w = (imgsBgView.rb_width - k5*2)/3;

        // 怎么赋值不好处理:要不放到一个数组中。
        for (int i =0 ; i<3; i++) {
            UIImageView *iv = [[UIImageView alloc] init];
            iv.layer.masksToBounds = YES;
            iv.layer.cornerRadius = 5;
            iv.frame = CGRectMake((iv_w+k5) * i, k5, iv_w, iv_w);
//            iv.backgroundColor = [UIColor blueColor];
            [imgsBgView addSubview:iv];
            
            [self.imgsArray addObject:iv];
        }
        
//        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        btn.frame = CGRectMake(10, imgsBgView.rb_bottom, 100, 50);
//        btn.backgroundColor = [UIColor brownColor];
//        [btn setTitle:@"测试" forState:(UIControlStateNormal)];
//        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//        [btn addTarget:self action:@selector(assignImgsAction) forControlEvents:(UIControlEventTouchUpInside)];
//        [_footerView addSubview:btn];
    }
    return _footerView;
}

- (void)assignImgsAction {
    XJLog(@"%@",self.imgsArray);
    
    NSArray *imgNameArr = @[@"share_wechat",@"share_qq",@"share_friends"];
    for (int i=0; i<self.imgsArray.count; i++) {
        UIImageView *iv = self.imgsArray[i];
        iv.image = [UIImage imageNamed:imgNameArr[i]];
    }
}


// 把iv装起来,等到请求到数据的时候方便拿到这些iv 在进行赋值。
- (NSMutableArray *)imgsArray {
    if (!_imgsArray) {
        _imgsArray = [NSMutableArray array];
    }
    return _imgsArray;
}

#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setuNav];
    
    [self requetData];
    
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = self.footerView;
    
}
- (void)setuNav{
    self.navTitle = @"详情";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:(@selector(cancelAction))];
}
- (void)cancelAction{
//    XJLog(@"取消");
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"是否取消申请" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if ([NSString isEmptyString:self.service_id]) {
            XJLog(@"self.service_id 为空无法操作:%@",self.service_id);
            
            return ;
        }
        
        [XJHttpTool requestCommunityServiceCancelWithURLPath:XJCommunityServiceCancel Service_id:self.service_id successH:^(id responseObjc) {
            XJLog(@"%@",responseObjc);
            
            NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
            if ([code isEqualToString:@"0"]) {
                
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [MBProgressHUD showError:responseObjc[@"message"]];
            }
        } failH:^(NSError *error) {
            [MBProgressHUD showError:[error localizedDescription]];
        }];
    }];
    
    [alertC addAction:cancelAction];
    [alertC addAction:sureAction];
    
    [self presentViewController:alertC animated:YES completion:nil];
}



#pragma mark - 三 请求数据

- (void)requetData {
//    self.dataArray = @[@"服务类别   维修",@"服务名称  煤气维修",@"联系人名称  张小花",@"联系人电话   188r87r8e7r8e7r8",@"提交时间   2018-6-1-eiruiu",@"状态   待处理"].mutableCopy;
    
    [XJHttpTool requestCommunityServiceDetailWithServiceId:self.serveId successH:^(id responseObjc) {
        
       
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSDictionary *dic = responseObjc[@"data"][@"service_info"];
            
            self.service_id = [NSString stringWithFormat:@"%@",dic[@"id"]];
            
            
            // 组装数据 service_info
            NSString *serveTypeStr = [NSString stringWithFormat:@"服务类别%@   ",dic[@"category_name"]];
            NSString *serveName = [NSString stringWithFormat:@"服务名称   %@",dic[@"name"]];
            NSString *personName = [NSString stringWithFormat:@"联系人称呼  %@",dic[@"linkman"]];
            NSString *phone = [NSString stringWithFormat:@"联系人电话   %@",dic[@"phone"]];
            
            
            NSString *timeStr = [self timeStrFromTimeStamp:[NSString stringWithFormat:@"提交时间  %@",dic[@"create_time"]]];
            NSString *stateSTr = [NSString stringWithFormat:@"状态  %@",dic[@"state_name"]];
            
            [self.dataArray addObject:serveTypeStr];
            [self.dataArray addObject:serveName];
            [self.dataArray addObject:personName];
            [self.dataArray addObject:phone];
            [self.dataArray addObject:timeStr];
            [self.dataArray addObject:stateSTr];
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                
                [self assignImgsAction]; // 假数据.服务器没有返回图片。
            });
        }else {
            XJLog(@"未能读取到数据,请检查失败原因:%@",responseObjc);
        }
        
    } fialH:^(NSError *error) {
        XJLog(@"%@",error);
    }];
    
    
}
#pragma mark - 代理 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"UITableViewCellId"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    
    XJLog(@"frame:%@",NSStringFromCGRect(cell.textLabel.frame));
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Private Method
// 时间戳转时间
- (NSString *)timeStrFromTimeStamp:(NSString *)timeStamp {
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];

    [fmt setDateStyle:NSDateFormatterMediumStyle];
    [fmt setTimeStyle:NSDateFormatterShortStyle];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue] / 1000.0];
    NSString *dateStr = [fmt stringFromDate:date];
    
    return dateStr;
}


#pragma mark - 请求数据返回的数据参考
/**
 
 {
 "data" : {
 "service_info" : {
 "id" : 26,
 "state" : 4,
 "linkman" : "back",
 "phone" : "15837694444",
 "category_id" : 1,
 "button" : {
 "text" : "删除",
 "code" : "delete"
 },
 "state_name" : "已取消",
 "community_id" : 8,
 "is_delete" : 0,
 "category_name" : "维修",
 "images" : "",
 "customer_id" : 7,
 "create_time" : 1527834352228,
 "name" : "维修",
 "content" : "啦啦啦"
 }
 },
 "code" : 0
 }
 */
@end
